/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package APIs;

import bllobjects.Account;
import dalfactory.SDALFactory;
import fsktms.request.RequestParameters;
import fsktms.request.RequestRespond;
import fsktms.request.api.RequestAPIGroup;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import utils.DataRow;
import utils.DataTable;

/**
 *
 * @author Mrkupi
 */
public class RelationAPIs extends RequestAPIGroup
{
    public RelationAPIs()
    {
        super("Relation");
    }

    public RequestRespond getFriendList(RequestParameters params)
    {
        RequestRespond result = new RequestRespond();
        try {
            String userName = params.getString("Name");
            
            ArrayList<Account> accs =   new ArrayList();
            DataTable dtRelation    =   SDALFactory.getInstance().getRelation().getFriendOf( userName );
            if( dtRelation != null )
            {
                for( int i = 0; i < dtRelation.size(); ++i )
                {
                    DataRow row         =   dtRelation.get( i );
                    String frName       =   row.get( "USERNAME1" ).equals(userName) ? row.get( "USERNAME2" ) : row.get( "USERNAME1" );
                    DataTable dtAccount =   SDALFactory.getInstance().getAccount().getAccountInfo( frName );
                    if( dtAccount != null )
                    {
                        DataRow rowFr   =   dtAccount.get( 0 );
                        accs.add( new Account(frName, rowFr.get("UserName"), Integer.parseInt(rowFr.get( "Experience" )), Integer.parseInt(rowFr.get( "Level" )), "0"));
                    }
                }
                
                result.set(RequestParameters.KEY_RESULT, accs);
            }
        } catch (RemoteException ex) {
            Logger.getLogger(RelationAPIs.class.getName()).log(Level.SEVERE, null, ex);
            result.setFailure(RequestParameters.VALUE_FAILURE_CODE_SERVER_ERROR, ex.getMessage());
        }
        
        return result;
    }
    
    public RequestRespond getRequestedFriend(RequestParameters params) 
    {
        RequestRespond result = new RequestRespond();
        String userName = params.getString("Name");
        try {
            DataTable dt = SDALFactory.getInstance().getRelation().getRequestedFriend(userName);
            ArrayList<String> requests  =   new ArrayList();
            if ( dt != null && dt.size() > 0)
            {
                for( int i = 0; i < dt.size(); ++i )
                {
                    DataRow row = dt.get(i);
                    requests.add( row.get( "RequestFrom" ) );
                }
            }
            
            result.set(RequestParameters.KEY_RESULT, requests);
        } catch (RemoteException ex) {
            Logger.getLogger(RelationAPIs.class.getName()).log(Level.SEVERE, null, ex);
            result.setFailure(RequestParameters.VALUE_FAILURE_CODE_SERVER_ERROR, ex.getMessage());
        }
        
        return result;
    }

    public RequestRespond sendRequestFriend(RequestParameters params) 
    {
        String userName = params.getString("Name");
        String friendName = params.getString("FriendName");
        RequestRespond result = new RequestRespond();
        try {
            result.set(RequestParameters.KEY_RESULT, SDALFactory.getInstance()
                    .getRelation().requestFriend(userName, friendName));
        } catch (RemoteException ex) {
            Logger.getLogger(RelationAPIs.class.getName()).log(Level.SEVERE, null, ex);
            result.setFailure(RequestParameters.VALUE_FAILURE_CODE_SERVER_ERROR, ex.getMessage());
        }
        
        return result;
    }

    public RequestRespond insertNewRelationShip(RequestParameters params)
    {
        String userName = params.getString("Name");
        String friendName = params.getString("FriendName");
        RequestRespond result = new RequestRespond();
        try {
            result.set(RequestParameters.KEY_RESULT,
                    SDALFactory.getInstance().getRelation().insertNewRelationShip(userName, friendName));
        } catch (RemoteException ex) {
            Logger.getLogger(RelationAPIs.class.getName()).log(Level.SEVERE, null, ex);
            result.setFailure(RequestParameters.VALUE_FAILURE_CODE_SERVER_ERROR, ex.getMessage());
        }
        
        return result;
    }

    public RequestRespond deleteRequestFriend(RequestParameters params)
    {
        String userName = params.getString("Name");
        String requester = params.getString("Requester");
        RequestRespond result = new RequestRespond();
        try {
            result.set(RequestParameters.KEY_RESULT,
                    SDALFactory.getInstance().getRelation().deleteRequest(userName, requester));
        } catch (RemoteException ex) {
            Logger.getLogger(RelationAPIs.class.getName()).log(Level.SEVERE, null, ex);
            result.setFailure(RequestParameters.VALUE_FAILURE_CODE_SERVER_ERROR, ex.getMessage());
        }
        
        return result;
    }
}
